﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    @* <link href="//unpkg.com/layui@2.9.20/dist/css/layui.css" rel="stylesheet"> *@

    <link href="~/lib/layui/css/layui.css" rel="stylesheet" />
    <script src="~/lib/layui/layui.js"></script>
    <script src="~/js/js.cookie.js"></script>
    <script src="~/js/moment.js"></script>
    <script src="~/lib/jquery/dist/jquery.js"></script>
</head>
<body>
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称：</label>
            <div class="layui-input-block">
                <input type="text" name="RolesName" lay-verify="required" placeholder="请输入角色名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权限：</label>
            <div class="layui-input-block">
                <div class="layui-input-block" id="checkPers">
                </div>
            </div>
            <!-- 目标div -->


        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
    @* <script src="//unpkg.com/layui@2.9.20/dist/layui.js"></script> *@


</body>
</html>
<script>
    layui.use(['form', 'laydate', 'util'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var util = layui.util;


        form.on('submit(demo1)', function (data) {
            var sa = [];
            $("[name='perCheck']:checked").each(function () {
                sa.push($(this).val())
            })
            let field = {
                rolesName: $("[name='RolesName']").val(),
                state: 0,
                ids: sa
            }
            console.log(JSON.stringify(field));
            $.ajax({
                url: '@ViewBag.Darius'+"/api/RBAC/craeteRoles",
                data: JSON.stringify(field),
                async: true,
                type: "post",
                dataType: "json",
                contentType: "application/json",
                success: function (res) {
                    console.log(123, res);
                    if (res.code > 0) {
                        layer.alert("添加成功", {
                            title: ''
                        });
                    }
                    else {
                        alert('提交失败')
                    }
                }
            })
            return false; // 阻止默认 form 跳转
        });

        $(function () {
            getPer();
        })
        function getPer() {
            $.ajax({
                url: '@ViewBag.Darius'+"/api/Users/PermissionSearch",
                type: "get",
                dataType: "json",
                contentType: "application/json",
                success: function (res) {
                    let a = 0; // 确保变量a已声明和初始化
                    res.data.forEach(function (item, i) {
                        $("#checkPers").append(`<input type="checkbox" name="perCheck" title="${item.permissionsName}" value="${item.permissionsId}">`);
                        a++
                    })
                    //重新渲染表单
                    form.render();
                }
            })
        }
    });

</script>

